package com.zxsc.zxmusic.fragment;


import org.simple.eventbus.EventBus;

import com.zxsc.zxmusic.R;
import com.zxsc.zxmusic.db.SongDb;
import com.zxsc.zxmusic.db.SongDb.OnLoginLister;
import com.zxsc.zxmusic.db.SongDb.OnRegisterLister;
import com.zxsc.zxmusic.manager.ThemeManager;
import com.zxsc.zxmusic.manager.ThemeManager.IThemeListener;
import com.zxsc.zxmusic.model.UserModel;
import com.zxsc.zxmusic.utils.SharedUtils;
import com.zxsc.zxmusic.utils.ToastUtils;
import com.zxsc.zxmusic.utils.UserUtils;

import android.content.Intent;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginFragment extends BaseFragment implements OnClickListener,IThemeListener {

	EditText etName;

	EditText etPwd;

	Button btnLogin;
	@Override
	protected View inflateView(LayoutInflater inflater, ViewGroup container) {
		View view=LayoutInflater.from(getActivity()).inflate(R.layout.activity_login, container,false);
		etName = (EditText) view.findViewById(R.id.login_username);
		etPwd = (EditText) view.findViewById(R.id.login_userpwd);
		btnLogin = (Button) view.findViewById(R.id.login_btnOk);
		btnLogin.setOnClickListener(this);
		 ThemeManager.with(getActivity()).registerListener(this);
		 initView();
		return view;
	}


	
	private void initView() {
		String name=SharedUtils.getString(getActivity(), "userName", "");
		String pwd=SharedUtils.getString(getActivity(), "userPwd", "");
		etName.setText(name);
		etPwd.setText(pwd);
	}
	
	@Override
	protected void onLoading() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void onLoadFinish() {
		// TODO Auto-generated method stub
		setLoginThem(ThemeManager.with(getActivity()).getCurrentColor());
	}
	
	@Override
	public void onClick(View v) {
		
		final String name= etName.getText().toString()
				.trim();
		final String pwd=etPwd.getText()
				.toString().trim();
		
		if(name==null || name.length()<=0){
			ToastUtils.show(getActivity(), "用户名不能为空！");
			return;
		}
		if(pwd==null || pwd.length()<=0){
			ToastUtils.show(getActivity(), "密码不能为空！");
			return;
		}else{
			if(pwd.length()<6){
				ToastUtils.show(getActivity(), "密码位数不能低于6位！");
				return;
			}
		}
		
			SongDb.login(getActivity(), etName.getText().toString().trim(), etPwd.getText().toString().trim(), new OnLoginLister() {
				
				@Override
				public void onSuccess(UserModel model) {
					
					SharedUtils.saveString(getActivity(), "userName", model.getUserName());
					SharedUtils.saveString(getActivity(), "userPwd", model.getUserPwd());
					ToastUtils.show(getActivity(), "登陆成功！");
					UserUtils.getInstance().setUserId(model.getUserId());
					UserUtils.getInstance().setUserName(model.getUserName());
					UserUtils.getInstance().setUserPwd(model.getUserPwd());
					UserUtils.getInstance().setSongMenuId(model.getSongMenuId());
					getActivity().setResult(10010,new Intent().putExtra("isLogin", true));
					EventBus.getDefault().post(new String("测试"),"up");
					getActivity().finish();
					
					
					
				}
				
				@Override
				public void onFail(String msg) {
					ToastUtils.show(getActivity(),msg,true);
				}
			});
	}
	private void setLoginThem(int color){
		etName.setHintTextColor(color);
		etPwd.setHintTextColor(color);
		GradientDrawable shapeDrawable=(GradientDrawable) btnLogin.getBackground();
		shapeDrawable.setColor(color);
//		btnLogin.setBackgroundColor(color);
	}

	@Override
	public void onThemeChange(int color) {
		setLoginThem(color);
	}
}
